/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package contatos.patterns.state;

import contatos.presenter.ManterContatoPresenter;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 *
 * @author felipe
 */
public abstract class ContatoState {
    
    ManterContatoPresenter presenter;
    
    public ContatoState(final ManterContatoPresenter presenter) {
        try {
            this.presenter = presenter;

            this.presenter.getView().getBtnFechar().addActionListener(new java.awt.event.ActionListener() {
                
                @Override
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    presenter.getView().setVisible(false);
                    presenter.getView().dispose();
                }
            });
            
            this.presenter.getView().getTxtOperadora().addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(final KeyEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            JTextField campo = (JTextField) e.getSource();
                            int posicaoCursor = campo.getCaretPosition();
                            campo.setText(campo.getText().toUpperCase());
                            if (posicaoCursor != campo.getCaretPosition()) {
                                campo.setCaretPosition(posicaoCursor);
                            }
                        }
                    });
                }
            });
            
            this.presenter.getView().getTxtTelefone().addFocusListener(new FocusAdapter() {
                
                @Override
                public void focusLost(FocusEvent e) {
                    presenter.identificaUfOperadora(presenter.getView().getTxtTelefone().getText());
                }
            });
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this.presenter.getView(), ex.getMessage());
        }
    }
    
    public abstract void incluir();
    
    public abstract void visualizar();
    
    public abstract void alterar();
    
}
